/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import CRUD.MemberHome;
import CRUD.PostHome;
import Utility.PostOperationsUtility;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import xmlMapping.Member;
import xmlMapping.Post;

/**
 *
 * @author Emyzzz
 */
public class DeletePost extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        System.out.println("here iam in delete post action");
        int postId=Integer.parseInt(request.getParameter("postid"));
        PostOperationsUtility postUtil=new PostOperationsUtility();
        
           Integer  useridsession= (Integer) request.getSession().getAttribute("id") ;
               
       PostHome ph=new PostHome();
        
        Post oldPost =ph.findById(postId);
        ph.releaseConnection();
               Integer me=oldPost.getMember().getMemberId();
     
               if(!Check_previlidge(useridsession, me))
               {
                   
                  return mapping.findForward("login");
               }
        
        
        
        
        
        postUtil.deletePost(postId);

        int pagenumber=Integer.parseInt(request.getParameter("pagenumber"));
        request.setAttribute("pagenumber", pagenumber);
        return mapping.findForward(SUCCESS);
    }
       public boolean Check_previlidge(Integer user,Integer memid)
    {
        
        boolean priv=false;
        Member mem=new  MemberHome().findById(user);
        System.out.println(" member on session is "+user+"member on post is "+memid);
        String type=mem.getType();
        System.out.println(" type of user "+type);
        if(memid==user||type.equals("adm"))
        {
            priv=true;
        }
        System.out.println(" perv is"+priv);
        return priv;
    }
}
